package com.cn.jx.manager.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.cn.jx.manager.po.User;

/**
 * <p>UserRepository 用户操作mapper层操作数据库的接口
 * <p>实现<{@link BaseRepository <tt>公用</tt>}}
 * 
 * @author <h1>jiangxu2  2019年11月21日</h1>
 * @version v1.11.20.2019
 * @since 1.0
 * 
 */
public interface UserRepository extends JpaRepository<User,String>,JpaSpecificationExecutor<User>{
	
	/**<p>定义HQL查询 根据登录名查询用户对象 有效的用户 {@link User} */
	@Query("from User  where loginName=:loginName and validFlg=1 and delFlg=0 ")
	public User findUserByLoginName(@Param(value = "loginName") String loginName);
	
	/**<p>定义HQL查询 根据员工编号查询用户对象 有效用户 {@link User} */
	@Query("from User  where employeeCode=:empcode and validFlg=1 and delFlg=0 ")
	public User findUserByEmpCode(@Param(value = "empcode") String empcode);
	
	/**<p>定义HQL查询 根据员工编号或登录名查询用户对象 有效用户 {@link User} */
	@Query("from User  where (loginName=:loginName or employeeCode=:loginName) and validFlg=1 and delFlg=0 ")
	public User findUserByLoginNameOrEmpCode(@Param(value = "loginName") String loginName);
	
	/**<p>定义HQL查询 查询时间段内注册的用户数量 不管有效无效  {@link User} */
	@Query("select count(1) from User  where createTime between :stime and :etime  ")
	public int findRegistUserNumToday(@Param(value = "stime") Date stime,@Param(value = "etime") Date etime);
	
	
	/**<p>定义HQL查询 根据员工编号查询用户对象 有效用户 {@link User} */
	@Query("select u.id from User u inner join UserOrgJob uoj on uoj.userId=u.id where uoj.orgId in(:orgIds) and u.validFlg=1 and u.delFlg=0 ")
	public List<String> findAllUserIdByOrgId(@Param(value="orgIds") List<String> orgIds);
}
